import express from 'express'
import sqlQuery from '../utils/sql';
const router = express.Router()
router.post('/getAddress', async (requset, reponese) => {
    const { uid } = requset.body
    let res = await sqlQuery(`select * from address where uid ='${uid}'`)
    reponese.send(res)
    // reponese.send(1)
})
router.post('/addAddress', async (requset, reponese) => {
    let { uid, name, phone, address, House_number } = requset.body
    let res = await sqlQuery(`insert into address (uid,name,phone,address,House_number) VALUE ('${uid}','${name}','${phone}','${address}','${House_number}') `)
    reponese.send("添加成功")
})
router.post('/updateAddress', async (requset, reponese) => {
    const { name, phone, address, House_number, aid } = requset.body
    let res = await sqlQuery(`update address set name='${name}',phone='${phone}',address='${address}',House_number='${House_number}' where aid='${aid}' `)
    if (res.changedRows === 1) {
        reponese.send({
            code: 200,
            msg: '修改成功'
        })
    } else {
        reponese.send({
            code: 404,
            msg: '修改失败'
        })
    }
})


// router.post('/delAddress', async (requset, reponese) => {
//     let { id } = requset.body
//     let res = await sqlQuery(`delete from address where id="${id}"`)
//     reponese.send("删除成功")
// })

module.exports = router